// 处理主题样式
export function handleThemeStyle(theme) {
  document.documentElement.style.setProperty('--el-color-primary', theme);

  for (let i = 1; i <= 9; i++) {
    document.documentElement.style.setProperty(`--el-color-primary-light-${i}`, getLightColor(theme, i / 10));
    document.documentElement.style.setProperty(`--el-color-primary-dark-${i}`, getDarkColor(theme, i / 10));
  }
}


// hex颜色转rgb颜色
export function hexToRgb(hex) {
  const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, (m, r, g, b) => {
    return r + r + g + g + b + b;
  });

  const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)] : null;
}


// rgb颜色转Hex颜色
export function rgbToHex(r, g, b) {
  return `#${[r, g, b].map(color => {
    const hex = color.toString(16);
    return hex.length === 1 ? `0${hex}` : hex;
  }).join('')}`;
}


// 变浅颜色值
export function getLightColor(color, level) {
  const rgb = hexToRgb(color).map(val =>
    Math.floor((255 - val) * level + val)
  );
  return rgbToHex(...rgb);
}


// 变深颜色值
export function getDarkColor(color, level) {
  const rgb = hexToRgb(color).map(val =>
    Math.floor(val * (1 - level))
  );
  return rgbToHex(...rgb);
}

